using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SpellingQuiz
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class QuizForm : System.Windows.Forms.Form
	{
		private SpellingUser user;
        private Random randomGenerator = new Random();

        private IContainer components;

		private System.Windows.Forms.Button QuizCompleteButton;
		private System.Windows.Forms.Label ScoreLabel;
		private System.Windows.Forms.Panel QuizPanel;
        private Button CloseButton;
        private Label timerLabel;
        private Timer quizTimer;
        private SpellingEntryPanel[] spellingEntries;
        private CheckBox inkInputCheckBox;

        DateTime startTime = DateTime.Now;

		public QuizForm( )
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
            CloseButton.Visible = false;
            QuizCompleteButton.Visible = true;

            user = Utilities.GetCurrentUser();
            this.Text = "Spelling Quiz [" + user.UserName + "]";

			CreateList();
            quizTimer.Enabled = true;
			//PopulateList();
            spellingEntries[0].Activate();
		}

		private void CreateList()
		{
			SpellingEntry currEntry;
			SpellingEntryPanel currPanel;
            int position, numSpelling = user.WordList.Count;
			spellingEntries = new SpellingEntryPanel[numSpelling];
            SpellingEntryPanel.ContainingForm = this;

			int baseTab = 1;

			for( int i = 0; i < numSpelling; i++ )
			{
				position = FindEmptySpot();
				currEntry = user.WordList[i] as SpellingEntry;
				currPanel = new SpellingEntryPanel( currEntry );
                currPanel.OrdinalPosition = i + 1;
				currPanel.Location = new System.Drawing.Point(1, 1 + (currPanel.Height * position));
				currPanel.TabIndex = baseTab + position;
				
				QuizPanel.Controls.Add( currPanel );
				spellingEntries[position] = currPanel;

			}
		}

		private int FindEmptySpot()
		{
			Boolean placed = false;
            int position = -1, max = user.WordList.Count;

			while(!placed)
			{
				position = randomGenerator.Next(0,max);
				if( spellingEntries[position] == null )
				{
					placed = true;
				}
			}

			return position;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.QuizCompleteButton = new System.Windows.Forms.Button();
            this.ScoreLabel = new System.Windows.Forms.Label();
            this.QuizPanel = new System.Windows.Forms.Panel();
            this.CloseButton = new System.Windows.Forms.Button();
            this.timerLabel = new System.Windows.Forms.Label();
            this.quizTimer = new System.Windows.Forms.Timer(this.components);
            this.inkInputCheckBox = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // QuizCompleteButton
            // 
            this.QuizCompleteButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.QuizCompleteButton.Font = new System.Drawing.Font("Arial Narrow", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.QuizCompleteButton.Location = new System.Drawing.Point(178, 328);
            this.QuizCompleteButton.Name = "QuizCompleteButton";
            this.QuizCompleteButton.Size = new System.Drawing.Size(75, 23);
            this.QuizCompleteButton.TabIndex = 0;
            this.QuizCompleteButton.Text = "Done!";
            this.QuizCompleteButton.Click += new System.EventHandler(this.QuizCompleteButton_Click);
            // 
            // ScoreLabel
            // 
            this.ScoreLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.ScoreLabel.AutoSize = true;
            this.ScoreLabel.Font = new System.Drawing.Font("Arial Narrow", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ScoreLabel.Location = new System.Drawing.Point(8, 16);
            this.ScoreLabel.Name = "ScoreLabel";
            this.ScoreLabel.Size = new System.Drawing.Size(0, 17);
            this.ScoreLabel.TabIndex = 1;
            // 
            // QuizPanel
            // 
            this.QuizPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.QuizPanel.AutoScroll = true;
            this.QuizPanel.Location = new System.Drawing.Point(8, 40);
            this.QuizPanel.Name = "QuizPanel";
            this.QuizPanel.Size = new System.Drawing.Size(335, 282);
            this.QuizPanel.TabIndex = 2;
            // 
            // CloseButton
            // 
            this.CloseButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.CloseButton.Font = new System.Drawing.Font("Arial Narrow", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.CloseButton.Location = new System.Drawing.Point(268, 328);
            this.CloseButton.Name = "CloseButton";
            this.CloseButton.Size = new System.Drawing.Size(75, 23);
            this.CloseButton.TabIndex = 3;
            this.CloseButton.Text = "Close Window";
            this.CloseButton.Click += new System.EventHandler(this.CloseButton_Click);
            // 
            // timerLabel
            // 
            this.timerLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.timerLabel.AutoSize = true;
            this.timerLabel.Location = new System.Drawing.Point(8, 328);
            this.timerLabel.Name = "timerLabel";
            this.timerLabel.Size = new System.Drawing.Size(0, 17);
            this.timerLabel.TabIndex = 4;
            // 
            // quizTimer
            // 
            this.quizTimer.Interval = 1000;
            this.quizTimer.Tick += new System.EventHandler(this.quizTimer_Tick);
            // 
            // inkInputCheckBox
            // 
            this.inkInputCheckBox.AutoSize = true;
            this.inkInputCheckBox.Location = new System.Drawing.Point(268, 13);
            this.inkInputCheckBox.Name = "inkInputCheckBox";
            this.inkInputCheckBox.Size = new System.Drawing.Size(80, 21);
            this.inkInputCheckBox.TabIndex = 5;
            this.inkInputCheckBox.Text = "Ink Input";
            this.inkInputCheckBox.UseVisualStyleBackColor = true;
            this.inkInputCheckBox.CheckedChanged += new System.EventHandler(this.inkInputCheckBox_CheckedChanged);
            // 
            // QuizForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
            this.ClientSize = new System.Drawing.Size(355, 357);
            this.ControlBox = false;
            this.Controls.Add(this.inkInputCheckBox);
            this.Controls.Add(this.timerLabel);
            this.Controls.Add(this.CloseButton);
            this.Controls.Add(this.QuizPanel);
            this.Controls.Add(this.ScoreLabel);
            this.Controls.Add(this.QuizCompleteButton);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.KeyPreview = true;
            this.Name = "QuizForm";
            this.ShowInTaskbar = false;
            this.Text = "Spelling Quiz";
            this.Load += new System.EventHandler(this.QuizForm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void GradeQuiz()
		{
			int correct = 0, wrong = 0;

			foreach( SpellingEntryPanel currEntry in spellingEntries )
			{
				if( currEntry.Evaluate() )
				{
					correct++;
				}
				else
				{
					wrong++;
				}
			}
			double score = ((correct*1.0)/(wrong+correct))*100.0;

			ScoreLabel.Text = String.Format("You got {0} correct and {1} wrong ({2:00}%).  {3}", correct, wrong, score, (score>=90?"GREAT JOB!":(score>=80?"Good work!":"Keep trying!")));

		}

		private void QuizCompleteButton_Click(object sender, System.EventArgs e)
		{
            QuizCompleteButton.Visible = false;
            quizTimer.Enabled = false;
			GradeQuiz();
			this.Refresh();
			Utilities.Speak( ScoreLabel.Text );
            CloseButton.Visible = true;
		}

        private void quizTimer_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = DateTime.Now.Subtract(startTime);
            timerLabel.Text = string.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds);
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            inkInputCheckBox.Checked = false;
            this.Close();
        }

        private void QuizForm_Load(object sender, EventArgs e)
        {

        }

        private void inkInputCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            SpellingEntryPanel.ShowInkEntry = inkInputCheckBox.Checked;
        }

	}
}
